<template>
  <div>
    <el-tree
      :data="permiss"
      show-checkbox
      ref="tree"
      node-key="permissionId"
      :default-expand-all="true"
      :default-checked-keys="defaultchecked"
      :props="{label: 'name'}">
    </el-tree>
    <el-button type="primary" plain @click="getCheckedKeys()" style="margin-top: 30px;">保存</el-button>
  </div>
</template>

<script>
export default {
  name: 'RolePermiss',
  data() {
    return {
      permiss: [],
      defaultchecked: [],
      form: {
        roleId: 0,
        permissionId: []
      }
    }
  },
  created() {
    this.form.roleId = this.$route.query.roleId
    this.getData()
    this.getPermiss()
  },
  methods: {
    getData() {
      this.axios.get('/rolePermission/getByRoleId',response => {
        if(response.obj) {
          this.form.roleId = response.obj.roleId
          this.defaultchecked = response.obj.permissionId.split(',').map(Number)
        }
      },{roleId: this.form.roleId})
    },
    getPermiss() {
      this.axios.get('/rolePermission/get-permiss',response => {
		  console.log(response.obj)
        this.permiss = response.obj
      })
    },
    getCheckedKeys() {
      this.form.permissionId = this.$refs.tree.getCheckedKeys()
      this.axios.post('/rolePermission/save',response => {
        this.$router.push('/lorole')
      },this.form)
    }
  }
}
</script>
<style scoped>
</style>
